<template>
  <div v-loading="loading">
    <div>
      <el-input placeholder="请输入日志类型后回车" @change="searchName" style="width:200px;"></el-input>
      <el-button @click="handleAdd" type="button">新增</el-button>
    </div>
    <el-table ref="singleTable"
              :data="tableData"
              :row-style="rowStyle"
              @current-change="handleCurrentChange"
              highlight-current-row
              size="mini"
              style="width: 100%">
      <el-table-column type="index"
                       width="50">
      </el-table-column>
      <el-table-column property="LoggerName"
                       label="日志类型"
                       width="250">
      </el-table-column>
      <el-table-column property="IsOpen"
                       label="是否打开"
                       width="250">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.IsOpen"
                     active-color="#00A854"
                     inactive-color="#F04134"
                     @change="changeSwitch(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right"
                       label="操作"
                       width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column property="Remarks"
                       label="备注">
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="editData">
        <el-form-item label="日志类型" size="mini">
          <el-input v-model="editData.LoggerName" auto-complete="off" :disabled="nameDisable" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="备注" size="mini">
          <el-input type="textarea" v-model="editData.Remarks" auto-complete="off" size="mini" :autosize="{ minRows: 5, maxRows: 4}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    name: 'LogType',
    data() {
      return {
        nameDisable: false,
        tableData: [],
        loading: false,
        allData: [],
        currentRow: {},
        editData: {},
        dialogFormVisible: false,
        url: this.$store.state.serviceUrl
      };
    },
    methods: {
      handleOk() {
        this.currentRow = this.editData;
        var ajaxParam = { type: "EditLogger", log: Base64.encode(JSON.stringify(this.currentRow)) };
        if (this.editData.mode == 1) {
          ajaxParam = { type: "AddLogger", log: Base64.encode(JSON.stringify(this.currentRow)) };
        }
        this.$http.post(this.url, ajaxParam, { emulateJSON: true }).then((m) => {
          this.$message({
            type: 'success',
            message: '保存成功!'
          });
          this.refreshData();
          this.dialogFormVisible = false;
        }, (e) => {
          this.$message(e.bodyText);
        });
      },
      handleEdit(row) {
        this.currentRow = row;
        this.editData = this.currentRow;
        this.editData.mode = 2;
        this.nameDisable = true;
        this.dialogFormVisible = true;
      },
      handleAdd() {
        this.editData = {};
        this.editData.mode = 1;
        this.nameDisable = false;
        this.dialogFormVisible = true;
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除该日志类型[' + row.LoggerName + '], 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.post(this.url, { type: "DelLogger", name: Base64.encode(row.LoggerName) }, { emulateJSON: true }).then((m) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.refreshData();
          }, (e) => {
            this.$message(e.bodyText);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleCurrentChange(val) {
        if (val) {
          this.currentRow = val;
        }
      },
      searchName(value) {
        this.tableData = this.allData.filter(
          (e) => {
            return e.LoggerName.indexOf(value) !== -1;
          });
      },
      rowStyle({ row, rowIndex }) {
      },
      changeSwitch(row) {
        this.$http.post(this.url, { type: "SetLoggerStatus", name: Base64.encode(row.LoggerName), isOpen: row.IsOpen }, { emulateJSON: true }).then((m) => {
          this.refreshData();
        }, (e) => {
          this.$message(e.bodyText);
        });
      },
      refreshData() {
        this.loading = true;
        this.$http.post(this.url, { type: "GetAllLogger" }, { emulateJSON: true }).then((m) => {
          this.allData = m.body;
          this.tableData = m.body;
          this.loading = false;
        }, (e) => {
          this.$message(e.bodyText);
          this.loading = false;
        });
      }
    },
    mounted() {
      this.refreshData();
    },
    filters: {
    }
  };
</script>
<style scoped>
  .el-table .el-table__row td {
    padding: 13px !important;
  }
</style>
